﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AngryNerds
{
    public class GameMenu : DrawableGameComponent
    {
        Texture2D newGameTexture, newGameSTexture,
            helpTexture, helpSTexture, creditsTexture,
            creditsSTexture, quitTexture, quitSTexture;
        Vector2 newGamePosition, helpPosition, creditsPosition,
            quitPosition;
        Rectangle newGameBounds, helpBounds, creditsBounds, quitBounds;
        bool newGameSelected=false, helpSelected=false, creditsSelected=false, quitSelected=false;
        MouseState mouseState;
        SpriteBatch spriteBatch;

        Texture2D TexturePointer, backTexture;

        SpriteFont spriteFont;

        Vector2 pointerCoordinate;

        private bool IS_CREDITS_MENU_SELECTED = false, IS_HELP_MENU_SELECTED=false;

        public GameMenu(Game game)
            : base(game)
        {
            LoadContent();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            ContentManager content = Game.Content;

            TexturePointer = content.Load<Texture2D>("pointer");

            backTexture = content.Load<Texture2D>("back");

            spriteFont = content.Load<SpriteFont>("Font");

            newGameTexture = content.Load<Texture2D>(@"menu\new");
            newGameSTexture = content.Load<Texture2D>(@"menu\new_s");
            helpTexture = content.Load<Texture2D>(@"menu\help");
            helpSTexture = content.Load<Texture2D>(@"menu\help_s");
            creditsTexture = content.Load<Texture2D>(@"menu\credits");
            creditsSTexture = content.Load<Texture2D>(@"menu\credits_s");
            quitTexture = content.Load<Texture2D>(@"menu\quit");
            quitSTexture = content.Load<Texture2D>(@"menu\quit_s");

            Rectangle windowRectangle = Game.Window.ClientBounds;

            newGamePosition = new Vector2((windowRectangle.Width - newGameTexture.Width) / 2, 100);
            helpPosition = new Vector2((windowRectangle.Width - helpTexture.Width) / 2, 200);
            creditsPosition = new Vector2((windowRectangle.Width - creditsTexture.Width) / 2, 300);
            quitPosition = new Vector2((windowRectangle.Width - quitTexture.Width) / 2, 400);

            newGameBounds = new Rectangle((int)newGamePosition.X, (int)newGamePosition.Y, newGameTexture.Width, newGameTexture.Height);
            helpBounds = new Rectangle((int)helpPosition.X, (int)helpPosition.Y, helpTexture.Width, helpTexture.Height);
            creditsBounds = new Rectangle((int)creditsPosition.X, (int)creditsPosition.Y, creditsTexture.Width, creditsTexture.Height);
            quitBounds = new Rectangle((int)quitPosition.X, (int)quitPosition.Y, quitTexture.Width, quitTexture.Height);

            newGameSelected = helpSelected = creditsSelected = quitSelected = false;
        }

        public override void Update(GameTime gameTime)
        {
            pointerCoordinate = new Vector2(mouseState.X - (TexturePointer.Width / 2), mouseState.Y - (TexturePointer.Height / 2));
            mouseState = Mouse.GetState();

            if (IS_CREDITS_MENU_SELECTED || IS_HELP_MENU_SELECTED)
            {
                KeyboardState keyboardState = Keyboard.GetState();
                if (keyboardState.IsKeyDown(Keys.Escape))
                {
                    IS_CREDITS_MENU_SELECTED = false;
                    IS_HELP_MENU_SELECTED = false;
                }
                return;
            }

            newGameSelected = helpSelected = creditsSelected = quitSelected = false;

            bool buttonPressed = mouseState.LeftButton == ButtonState.Pressed;

            if (newGameBounds.Contains(mouseState.X, mouseState.Y))
            {
                newGameSelected = true;

                if (buttonPressed)
                    NewGamePressed();
            }
            else if (helpBounds.Contains(mouseState.X, mouseState.Y))
            {
                helpSelected = true;

                if (buttonPressed)
                    IS_HELP_MENU_SELECTED = true;
                    //HelpPressed();
            }
            else if (creditsBounds.Contains(mouseState.X, mouseState.Y))
            {
                creditsSelected = true;

                if (buttonPressed)
                    IS_CREDITS_MENU_SELECTED = true;
                    //CreditsPressed();
            }
            else if (quitBounds.Contains(mouseState.X, mouseState.Y))
            {
                quitSelected = true;

                if (buttonPressed)
                    QuitPressed();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            DrawBack();

            if (IS_CREDITS_MENU_SELECTED)
            {
                spriteBatch.Begin();
                spriteBatch.DrawString(spriteFont, "Guney Guner", new Vector2(600, 300), Color.Magenta);
                spriteBatch.DrawString(spriteFont, "Tugay Mehmet Ilbay", new Vector2(600, 350), Color.Magenta);
                spriteBatch.DrawString(spriteFont,"Umut Balkisli",new Vector2(600,400),Color.Magenta);
                spriteBatch.DrawString(spriteFont,"Yigit Bekir Kaya",new Vector2(600,450),Color.Magenta);
                spriteBatch.DrawString(spriteFont,"Press ESC To Display Menu",new Vector2(700,500),Color.Red);
                spriteBatch.End();
                return;
            }
            else if (IS_HELP_MENU_SELECTED)
            {
                spriteBatch.Begin();
                spriteBatch.DrawString(spriteFont,"Press Left or Right to move catapult",new Vector2(500,300),Color.MediumPurple);
                spriteBatch.DrawString(spriteFont,"Press Space to pull catapult's arm",new Vector2(500,350),Color.MediumPurple);
                spriteBatch.DrawString(spriteFont,"Release Space to throw bird",new Vector2(500,400),Color.MediumPurple);
                spriteBatch.DrawString(spriteFont, "Press ESC To Display Menu", new Vector2(700, 500), Color.Red);
                spriteBatch.End();
                return;
            }

            spriteBatch.Begin();

            if (newGameSelected)
                spriteBatch.Draw(newGameSTexture, newGamePosition, Color.White);
            else
                spriteBatch.Draw(newGameTexture, newGamePosition, Color.White);

            if (helpSelected)
                spriteBatch.Draw(helpSTexture, helpPosition, Color.White);
            else
                spriteBatch.Draw(helpTexture, helpPosition, Color.White);

            if (creditsSelected)
                spriteBatch.Draw(creditsSTexture, creditsPosition, Color.White);
            else
                spriteBatch.Draw(creditsTexture, creditsPosition, Color.White);

            if (quitSelected)
                spriteBatch.Draw(quitSTexture, quitPosition, Color.White);
            else
                spriteBatch.Draw(quitTexture, quitPosition, Color.White);

            spriteBatch.End();
            DrawPointer();
        }

        void NewGamePressed()
        {
            AngryGame game = (AngryGame)Game;

            game.NewGame();
        }

        void HelpPressed()
        {
        }

        void CreditsPressed()
        {
        }

        void QuitPressed()
        {
            Game.Exit();
        }

        private void DrawPointer()
        {
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            spriteBatch.Draw(TexturePointer, pointerCoordinate, Color.White);
            spriteBatch.End();
        }

        private void DrawBack()
        {
            spriteBatch.Begin();
            spriteBatch.Draw(backTexture, new Rectangle(0, 0, Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height), Color.White);
            spriteBatch.End();
        }
    }
}
